package com.example.himalaya.adapters;

import android.annotation.SuppressLint;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.R;
import com.example.himalaya.utils.LogUtil;
import com.squareup.picasso.Picasso;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import java.util.ArrayList;
import java.util.List;

public class AlbumListRvCardAdapter extends RecyclerView.Adapter<AlbumListRvCardAdapter.ViewHolder> {

    private static final String TAG = "RecommendListRvAdapter";
    private onItemClickListener mOnItemClickListener =null;
    private OnAlbumItemLongClickListener mOnAlbumItemLongClickListener;


    public class ViewHolder extends RecyclerView.ViewHolder {
        private final ImageView mAlbumCoverIv;
        private final TextView mAlbumTitleTv;
        private final TextView mAlbumPlayCountTv;
        private final TextView mAlbumContentSizeTv;
        private final TextView mAlbumAuthorTv;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mAlbumCoverIv = itemView.findViewById(R.id.album_cover_iv);
            mAlbumTitleTv = itemView.findViewById(R.id.album_title_tv);
            mAlbumPlayCountTv = itemView.findViewById(R.id.album_play_count);
            mAlbumContentSizeTv = itemView.findViewById(R.id.album_content_size);
            mAlbumAuthorTv = itemView.findViewById(R.id.album_author_tv);
        }

        /**
         * 视图控件设置相应的数据
         * @param album 要显示的专辑
         */
        @SuppressLint("SetTextI18n")
        public void setData(Album album) {
            String coverUrlLarge = album.getCoverUrlLarge();
            if (!TextUtils.isEmpty(coverUrlLarge)) {
                Picasso.with(itemView.getContext()).load(coverUrlLarge).into(mAlbumCoverIv);
            }else {
                mAlbumCoverIv.setImageResource(R.mipmap.logo);
            }
            mAlbumTitleTv.setText(album.getAlbumTitle());
            String playCountString = null;
            long playCount = album.getPlayCount();
            if (playCount<10000) {
                playCountString = String.valueOf(playCount);
            } else if (playCount < 100000000) {
                playCountString = (playCount / 10000) + "万";
            } else {
                playCountString = (playCount / 100000000) + "亿";
            }
            mAlbumPlayCountTv.setText(playCountString);
            mAlbumContentSizeTv.setText(album.getIncludeTrackCount()+"集");
            mAlbumAuthorTv.setText(album.getAnnouncer().getNickname());
        }
    }




    private List<Album> mAlbumList = new ArrayList<>();

    /**
     * 设置推荐数据
     * @param albumList 专辑列表
     */
    @SuppressLint("NotifyDataSetChanged")
    public void setData(List<Album> albumList) {
        if (albumList != null) {
            mAlbumList.clear();
            mAlbumList.addAll(albumList);
        }
        //通知数据发生变化，更新UI
        notifyDataSetChanged();
    }

    public int getDataSize() {
        return mAlbumList.size();
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //加载View
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.fragment_recommend_rv_item, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        //设置数据
        holder.itemView.setTag(position);
        holder.setData(mAlbumList.get(position));
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LogUtil.d(TAG,"itemView被点击 -->"+v.getTag());
                if (mOnItemClickListener != null) {
                    int clickPosition = (int) v.getTag();
                    mOnItemClickListener.OnItemClick(clickPosition,mAlbumList.get(clickPosition));
                }
            }
        });
        holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnAlbumItemLongClickListener != null) {
                    int clickPosition = (int) v.getTag();
                    mOnAlbumItemLongClickListener.onAlbumItemLongClick(clickPosition,mAlbumList.get(clickPosition));
                }
                return true;
            }
        });
    }


    @Override
    public int getItemCount() {
        //返回要显示的个数
        if (mAlbumList != null) {
            return mAlbumList.size();
        }
        return 0;
    }

    public void setOnItemClickListener(onItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public interface onItemClickListener {
        /**
         * itemView被点击了
         * @param position 位置索引
         * @param album 被点击的专辑
         */
        void OnItemClick(int position, Album album);
    }


    public interface OnAlbumItemLongClickListener{
        void onAlbumItemLongClick(int position, Album album);
    }
    public void setOnAlbumItemLongClickListener(OnAlbumItemLongClickListener onAlbumItemLongClickListener){
        this.mOnAlbumItemLongClickListener = onAlbumItemLongClickListener;
    }

}
